package com.dlc.garbagerecyclebox.bean;

import android.text.TextUtils;

import com.dlc.commonbiz.base.util.GsonUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by guoshuifang on 2019/8/9.
 * TODO 传给后台的心跳数据
 */
public class HeartBeat {
    public String event;//默认 = heartbeat
    public String macno;
    public String versionCode;//当前版本号
    public String versionName;//当前版本名称
    public List<DeviceinfoBean> deviceinfo;

    public void addDeviceinfoBean(DeviceinfoBean deviceinfoBean) {
        if (deviceinfo == null || deviceinfo.size() == 0) {
            deviceinfo = new ArrayList<>();
            deviceinfo.add(deviceinfoBean);
        } else {
            boolean isUpdate = false;
            for (int i = 0; i < deviceinfo.size(); i++) {
                DeviceinfoBean deviceinfoBean1 = deviceinfo.get(i);
                if (deviceinfoBean.boxType == deviceinfoBean1.boxType) {
                    isUpdate = true;
                    deviceinfoBean1 = deviceinfoBean.clone();
                }
            }
            if (!isUpdate) {
                deviceinfo.add(deviceinfoBean);
            }
        }

    }

    public static class DeviceinfoBean implements Cloneable {
        public double temperatureValue;//温度值 float

        public int boxType;//箱子类型,1-6

        public float realTimeAmount;//实时重量值 float 单位KG

        public float verticalRangingHeight;//垂直测距高度 float 单位cm

        public boolean isBoxFullTransversePhotoelectricOne;//箱满横向光电1 bolean

        public boolean isBoxFullTransversePhotoelectricTwo;//箱满横向光电2 bolean

        public boolean isBoxFullTransversePhotoelectricThree;//箱满横向光电3 bolean

        public float totalAmount;//（副柜）累计重量值 float ,（主柜）累计饮料瓶数量

        public int isSmokeSensing;//烟雾感应 1=烟感异常

        public boolean isOpenHarmful;//有害垃圾门是否打开，
        
        public boolean isUpperHatchLimit;//上仓门是否打开
        
        public boolean isUpperHatchLowerLimit;//下仓门是否打开
        
        public boolean isELockUpperLowerLimit;//下仓门锁是否打开
        
        
        
        public boolean isLightingLampInBox;//灯否打开

        public List<String> faultCode;//故障码

        /**
         * faultCode 说明
         * open_door_overtime	开门超时
         * open_door_too_short	开门过短
         * open_door_motor	开门电机故障
         * close_door_overtime	关门超时
         * close_door_too_short	关门过短
         * close_door_motor	关门电机故障
         * hatch_door_left_anti_clip_hand	上仓门-左防夹手异常（长期触发）
         * hatch_door_right_anti_clip_hand	上仓门-右防夹手异常（长期触发）
         * conveyor_belt_motor_over_current	传送带-电机过流（结冰）
         * lower_storehouse_door_open_door_over_time	下仓门-开门异常（超时）
         * lower_storehouse_door_open_door	下仓门-关门异常(只关一半的情况)
         * visual_communication	视觉工控机通信故障
         * electronic_scale_communication	电子秤通信故障
         * electronic_scale_hardware	电子秤硬件故障
         * vertical_ranging_communication	垂直测距通信故障
         * vertical_ranging_hardware	垂直测距硬件故障
         * rs485_communication	RS485通信异常
         * communication_timeout	访问下位机通讯超时
         * failed_to_open_camera 打开摄像头失败
         */
        @Override
        public String toString() {
            return "DeviceinfoBean{"
                    + "temperatureValue="
                    + temperatureValue
                    + ", boxType="
                    + boxType
                    + ", realTimeAmount="
                    + realTimeAmount
                    + ", verticalRangingHeight="
                    + verticalRangingHeight
                    + ", isBoxFullTransversePhotoelectricOne="
                    + isBoxFullTransversePhotoelectricOne
                    + ", isBoxFullTransversePhotoelectricTwo="
                    + isBoxFullTransversePhotoelectricTwo
                    + ", isBoxFullTransversePhotoelectricThree="
                    + isBoxFullTransversePhotoelectricThree
                    + ", totalAmount="
                    + totalAmount
                    + ", isSmokeSensing="
                    + isSmokeSensing
                    + ", faultCode="
                    + GsonUtil.getInstance().parseObjToJsonStr(faultCode)
                    + '}';
        }

        @Override
        public DeviceinfoBean clone() {
            DeviceinfoBean deviceinfoBean = null;
            try {
                deviceinfoBean = (DeviceinfoBean) super.clone();
                if (faultCode != null) {
                    deviceinfoBean.faultCode = new ArrayList<>(faultCode);
                }
            } catch (CloneNotSupportedException e) {
                e.printStackTrace();
            }
            return deviceinfoBean;
        }
    }

    public interface FaultCode {
        String OPEN_DOOR_OVERTIME = "open_door_overtime ";
        String OPEN_DOOR_TOO_SHORT = "open_door_too_short ";
        String OPEN_DOOR_MOTOR = "open_door_motor ";

        String CLOSE_DOOR_OVERTIME = "close_door_overtime ";
        String CLOSE_DOOR_TOO_SHORT = "close_door_too_short ";
        String CLOSE_DOOR_MOTOR = "close_door_motor ";

        String HATCH_DOOR_LEFT_ANTI_CLIP_HAND = "hatch_door_left_anti_clip_hand";
        String HATCH_DOOR_RIGHT_ANTI_CLIP_HAND = "hatch_door_right_anti_clip_hand";
        String CONVEYOR_BELT_MOTOR_OVER_CURRENT = "conveyor_belt_motor_over_current";
        String LOWER_STOREHOUSE_DOOR_OPEN_DOOR_OVER_TIME = "lower_storehouse_door_open_door_over_time";
        String LOWER_STOREHOUSE_DOOR_OPEN_DOOR = "lower_storehouse_door_open_door";
        String VISUAL_COMMUNICATION = "visual_communication";
        String ELECTRONIC_SCALE_COMMUNICATION = "electronic_scale_communication";
        String ELECTRONIC_SCALE_HARDWARE = "electronic_scale_hardware";
        String VERTICAL_RANGING_COMMUNICATION = "vertical_ranging_communication";
        String VERTICAL_RANGING_HARDWARE = "vertical_ranging_hardware";
        String RS485_COMMUNICATION = "rs485_communication";
        String COMMUNICATION_TIMEOUT = "communication_timeout";
        String FAILED_TO_OPEN_CAMERA = "failed_to_open_camera";
    }

    public boolean notEqualsData(HeartBeat heartBeat) {
        if (heartBeat != null) {
            if (TextUtils.equals(event, heartBeat.event) && TextUtils.equals(macno, heartBeat.macno)) {
                if (heartBeat.deviceinfo.size() != deviceinfo.size()) {
                    return true;
                }
                for (int i = 0; i < heartBeat.deviceinfo.size(); i++) {
                    DeviceinfoBean deviceinfo1 = heartBeat.deviceinfo.get(i);
                    for (int j = 0; j < deviceinfo.size(); j++) {
                        DeviceinfoBean deviceinfo2 = heartBeat.deviceinfo.get(j);
                        if (deviceinfo1.boxType == deviceinfo2.boxType) {
                            if (deviceinfo1.faultCode.size() != deviceinfo2.faultCode.size()) {
                                return true;
                            }
                            if (!TextUtils.equals(GsonUtil.getInstance().parseObjToJsonStr(deviceinfo1.faultCode),
                                    GsonUtil.getInstance().parseObjToJsonStr(deviceinfo2.faultCode))) {
                                return true;
                            }
                            if (deviceinfo1.isSmokeSensing != deviceinfo2.isSmokeSensing) {
                                return true;
                            }
                        }
                    }
                }
            }
        }
        return false;
    }

}
